﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;

namespace H2.NET.Web.Pages
{
    public partial class TabCCC : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }

                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }

        private string CustomerCode
        {
            get
            {
                return ViewState["CustomerCode"] == null ? "" : (string)ViewState["CustomerCode"];

            }
            set { ViewState["CustomerCode"] = value; }
        }

        private string CustomerName
        {
            get
            {
                return ViewState["CustomerName"] == null ? "" : (string)ViewState["CustomerName"];

            }
            set { ViewState["CustomerName"] = value; }
        }

        public string ProductionLine
        {
            get
            {
                return ViewState["ProductionLine"] == null ? "" : (string)ViewState["ProductionLine"];

            }
            set { ViewState["ProductionLine"] = value; }
        }

        public string Color
        {
            get
            {
                return ViewState["Color"] == null ? "" : (string)ViewState["Color"];

            }
            set { ViewState["Color"] = value; }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }

        public bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }
        #endregion

        #region 事件 Load (页面)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Status.IsUsed)
            {
                return;
            }

            bool flg = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
            gv_CCC_1.DynamicColumn.Add(new BoundColumn2("00", "客户编号", BoundColumnType2.TextBox, 60, Align.Left, flg, "CustomerCode", InputType.None, null));
            gv_CCC_1.DynamicColumn.Add(new BoundColumn2("01", "客户名称", BoundColumnType2.TextBox, 100, Align.Left, flg, "CustomerName", InputType.None, null));
            gv_CCC_1.DynamicColumn.Add(new BoundColumn2("02", "产品线", BoundColumnType2.TextBox, 60, Align.Left, flg, "ProductionLine", InputType.None, null));
            gv_CCC_1.DynamicColumn.Add(new BoundColumn2("03", "颜色", BoundColumnType2.TextBox, 60, Align.Left, flg, "Color", InputType.None, null));
            gv_CCC_1.DynamicColumn.Add(new BoundColumn2("04", "包装", BoundColumnType2.TextBox, 60, Align.Left, flg, "Package", InputType.None, null));
            gv_CCC_1.DynamicColumn.Add(new BoundColumn2("05", "数量", BoundColumnType2.TextBox, 60, Align.Left, flg, "Amount", InputType.None, null));
            gv_CCC_1.DynamicColumn.Add(new BoundColumn2("06", "销售", BoundColumnType2.TextBox, 60, Align.Left, flg, "SalesPerson", InputType.None, null));
            gv_CCC_1.DynamicColumn.Add(new BoundColumn2("07", "批号", BoundColumnType2.TextBox, 50, Align.Left, flg, "BatchNo", InputType.None, null));
            gv_CCC_1.DynamicColumn.Add(new BoundColumn2("08", "Barcode", BoundColumnType2.TextBox, 40, Align.Left, flg, "Barcode", InputType.None, null));
            gv_CCC_1.DynamicColumn.Add(new BoundColumn2("09", "电脑单号", BoundColumnType2.TextBox, 60, Align.Left, flg, "PCNo", InputType.None, null));
            gv_CCC_1.DynamicColumn.Add(new BoundColumn2("10", "备注", BoundColumnType2.TextBox, 120, Align.Left, flg, "Remarks", InputType.None, null));
            
            if (!Page.IsPostBack)
            {
                InitControls();
            }
        }
        #endregion

        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }

            foreach (mdc_CCC ccc in (List<mdc_CCC>)gv_CCC_1.DataSource)
            {
                //if (string.IsNullOrEmpty(ccc.PCNo))
                //{
                //    continue;
                //}

                ccc.Process  = TaskAPI.Process;
                ccc.Incident = TaskAPI.Incident;
                ccc.UserName = UserAPI.UserName;
                ccc.Time     = System.DateTime.Now;
                ccc.Status   = 1;
                Storage.Save(ccc);
                //string s = ccc.ProductionLine + ccc.Color;
                TaskAPI["ProductionLine"] = ccc.ProductionLine;
                TaskAPI["Color"] = ccc.Color;
            }
        }
        #endregion

        #region 方法 InitControls(初始化)
        public void InitControls()
        {
            gv_CCC_1.Key = Key;
            if (TaskAPI.IsInitiate)
            {
                List<mdc_CCC> ccc_s = new List<mdc_CCC>();
                for (int i = 0; i < 1; i++)
                {
                    ccc_s.Add(new mdc_CCC());
                    ccc_s[i].No = i + 1;
                }
                gv_CCC_1.DataSource = ccc_s;
            }
            else
            {
                IList<mdc_CCC> ccc_s = (mdc_CCC.process == TaskAPI.Process & mdc_CCC.incident == TaskAPI.Incident & mdc_CCC.status == 1).List<mdc_CCC>();
                if (ccc_s.Count == 0)
                {
                    for (int i = 0; i < 1; i++)
                    {
                        ccc_s.Add(new mdc_CCC());
                        ccc_s[i].No = i + 1;
                    }
                }
                
                gv_CCC_1.DataSource = ccc_s;
            }
        }
        #endregion

        #region 方法 gv_CCC_1 数据取得
        protected void gv_CCC_1_DataBinding(object sender, EventArgs e)
        {
            //if (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit && gv_CCC_1.ChangedColumnIndex == 9)
            if (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit && (gv_CCC_1.ChangedColumnIndex == 0 || gv_CCC_1.ChangedColumnIndex == 1))

            {
                if (TaskAPI.Process == "MTO Base Code Application")
                {


                    List<mdc_CCC> ccc_s = gv_CCC_1.DataSource as List<mdc_CCC>;

                    foreach (mdc_CCC ccc in ccc_s)
                    {
                        m_Customer ncc = (m_Customer.customerCode == ccc.CustomerCode).ListFirst<m_Customer>();
                        if (ncc != null)
                        {
                            ccc.CustomerName = ncc.CustomerName;
                        }
                        else
                        {
                            ccc.CustomerName = "";
                        }
                    }
                }
                else
                {

                }
            }
         }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!(Status.IsUsed && TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit))
            {
                return new List<string>();
            }

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();
            foreach (mdc_CCC ccc in (List<mdc_CCC>)gv_CCC_1.DataSource)
            {
                regs.Add(new Regular(RegularExp.NotEmpty, ccc.ProductionLine, "&nbsp;&nbsp;行1: 产品线为必填"));
                regs.Add(new Regular(RegularExp.NotEmpty, ccc.Color, "&nbsp;&nbsp;行1: 颜色为必填"));
                regs.Add(new Regular(RegularExp.NotEmpty, ccc.Package, "&nbsp;&nbsp;行1: 包装为必填"));
                regs.Add(new Regular(RegularExp.NotEmpty, ccc.CustomerCode, "&nbsp;&nbsp;行1: 客户编号为必填"));
                regs.Add(new Regular(RegularExp.NotEmpty, ccc.CustomerName, "&nbsp;&nbsp;行1: 客户名称为必填"));
            }

            vald_s.AddRange(Validation(regs));

            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>" + Status.Name + "</b></font>");
                vald_s.Add("&nbsp;&nbsp;-----------------------------------------");
            }

            return vald_s;
        }
        #endregion 
    }
}
